package com.zdx.yswl;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.baidu.mapapi.SDKInitializer;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.zdx.yswl.tool.IHandlerLikeNotify;
import com.zdx.yswl.tool.KLog;
import com.zdx.yswl.tool.KStringTool;
import com.zdx.yswl.ui.activity.ConsultantActivity;

public class App extends Application implements IHandlerLikeNotify
{
	/** App  */
	private static App share ;
	
	/** 用户登录后 userId */
//	public static String userId = "";

    /** 回调方法 **/
    private IHandlerLikeNotify currentNotifyer;
	
	/** 获取实例 */
	public static App getInstance() {
		return share;
	}

	@Override
	public void onCreate()
    {
		super.onCreate();
		share = this;
		
		KLog.isShow = false;
		initBaiduMap();
		initImageLoader(share);//第三方图片异步加载 
        currentNotifyer = null;
	}

    /**
     * 底层回调方法
     * @param what
     * @param uchType
     * @param channel
     * @param obj
     */
    public synchronized void onJniNotify(int what, int uchType, int channel, Object obj)
    {
        if (null != currentNotifyer)
        {
            currentNotifyer.onNotify(what, uchType, channel, obj);
            Log.e("Notifyer",
                    "What: " + what + "  "
                     + "Arg1: " + uchType + "  "
                     + "Arg2: " + channel + "  " /* + "Obj: " + obj*/);
        }
        else
        {
            Log.e("APP", "null notifyer");
        }
    }

    /**
     * 修改当前显示的 Activity 引用
     * @param currentNotifyer
     */
    public void setCurrentNotifyer(IHandlerLikeNotify currentNotifyer)
    {
        this.currentNotifyer = currentNotifyer;
    }

    /**
     * 获取路径
     * @return
     */
	public String getHostPath()
    {
		return Constant.HOST_PATH;
	}
	
	/**
	 * 初始化百度地图
	 */
	public void initBaiduMap()
    {
		SDKInitializer.initialize(this);
	}
	
	/**
	 * 记录当前时间
	 * @param context
	 * @param currentTimeMillis
	 */
	public void recordCurrentTime (Context context,Long currentTimeMillis ,String id )
    {
		SharedPreferences sp = context.getSharedPreferences("praise_time", Activity.MODE_PRIVATE);
		Editor  edit = sp.edit();
		edit.putLong(id, System.currentTimeMillis());
		edit.commit();
	}
	/**
	 * 取出点赞的时间
	 * @param context
	 */
	public Long getPraiseTime (Context context,String id){
		SharedPreferences sp = context.getSharedPreferences("praise_time", Activity.MODE_PRIVATE);
		long praise = sp.getLong(id, 0);
		return praise;
	}
	
	 /** 记录用户名 密码
	 * @param context
	 */
	public void recordUserLoginStatus (Context context,String userName,String userPwd){
		SharedPreferences sp = context.getSharedPreferences("loginStatus", Activity.MODE_PRIVATE);
		Editor  edit = sp.edit();
		edit.putString("userName", userName);
		edit.putString("userPwd", userPwd);
		edit.commit();
	}
	/** 记录用户名id
	 * @param context
	 */
	public void recordUserId (Context context,String userId){
		SharedPreferences sp = context.getSharedPreferences("loginStatus", Activity.MODE_PRIVATE);
		Editor  edit = sp.edit();
		edit.putString("userId", userId);
		edit.commit();
	}
	/**
	 * 得到用户登录成功返回的id
	 * @param context
	 * @return
	 */
	public String getUserId(Context context){
		SharedPreferences sp = context.getSharedPreferences("loginStatus", Activity.MODE_PRIVATE);
		String name = sp.getString("userId", "");
		return name;
	}

	/**
	 * 得到用户名字
	 * @param context
	 * @return
	 */
	public String getUserName(Context context)
	{
		SharedPreferences sp = context.getSharedPreferences("loginStatus", Activity.MODE_PRIVATE);
		String name = sp.getString("userName", "");
		return name;
	}
	
	/** 用户是否登录 */
	public  boolean isLogin(Context context){
		if (KStringTool.isEmpty(getUserId(context))) {
			return false;
		}
		return true;
	}

    @Override
    public void onNotify(int what, int arg1, int arg2, Object obj)
    {

    }
    
    
	public static void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you may tune some of them,
		// or you can create default configuration by
		//  ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}
}
